﻿using System;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class List12Record : BiffRecord
    {
        private FrtHeader frtHeader = new FrtHeader {_rt = 0x0877};
        private uint idList;
        internal List12RecordItem rgb;

        public List12Record(List12RecordItem item, int id)
        {
            rgb = item;
            idList = (uint) id;
        }

        public List12Record()
        {
        }

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.List12; }
        }

        protected override void ReadContent(BiffReaderContext context, int length)
        {
#if DEBUG
            var position = context.Reader.BaseStream.Position;
#endif
            frtHeader = new FrtHeader();
            frtHeader.Read(context.Reader);
            Contract.Assert(frtHeader._rt == 0x0877);
            var lsd = (List12RecordItemType) context.ReadUInt16();
            idList = context.ReadUInt32();
            switch (lsd)
            {
                case List12RecordItemType.List12BlockLevel:
                    rgb = new List12BlockLevel();
                    break;
                case List12RecordItemType.List12DisplayName:
                    rgb = new List12DisplayName();
                    break;
                case List12RecordItemType.List12TableStyleClientInfo:
                    rgb = new List12TableStyleClientInfo();
                    break;
                default:
                    throw new NotSupportedException();
            }
            rgb.Read(context.Reader);
#if DEBUG
            var position2 = context.Reader.BaseStream.Position;
            Contract.Assert((position2 - position) == length);
#endif
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            frtHeader.Write(context);
            Contract.Assert(frtHeader._rt == 0x0877);
            context.Write((ushort) rgb.Type);
            context.Write(idList);
            rgb.Write(context.Writer);
        }
    }
}